Redeclaration of variable [different scope]

Hi all,

I was wondering if there is a way for Perl to give me a warning if I
redeclare a variable in a different scope (and thus masking the outer
one). Just spent some time debugging this (which was obviously not my
intention to give two variables the same name)...and I guess it is a
silly mistake that I would do again and again. :-)

i.e., This gives a warning:

my $foo = 0;
my $foo = 1;

But this works:

my $foo = 0;
{
my $foo = 1;
}

Thanks!

Ray

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Raymond Wan [ Do, 27 Januar 2011 10:51 ] [ ID #2053956 ]

Re: Redeclaration of variable [different scope]

On Jan 27, 1:51=A0am, r.... [at] aist.go.jp (Raymond Wan) wrote:
> Hi all,
>
> I was wondering if there is a way for Perl to give me a warning if I
> redeclare a variable in a different scope (and thus masking the outer
> one). =A0Just spent some time debugging this (which was obviously not my
> intention to give two variables the same name)...and I guess it is a
> silly mistake that I would do again and again. =A0:-)
>
> i.e., =A0This gives a warning:
>
> my $foo =3D 0;
> my $foo =3D 1;
>
> But this works:
>
> my $foo =3D 0;
> {
> =A0 my $foo =3D 1;
>
> }


Not that I'm aware of but you could use B::Xref to
generate a cross reference.

perl -MO=3DXref -wle 'my $foo=3D0; { my $foo =3D 1; }'
....
$foo i1, i1 <---- 2 instances of '$foo'

--
Charles DeRykus


--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
derykus [ Do, 27 Januar 2011 12:26 ] [ ID #2053960 ]

Re: Redeclaration of variable [different scope]

Hi Charles,

On Thu, Jan 27, 2011 at 20:26, C.DeRykus <derykus [at] gmail.com> wrote:
> On Jan 27, 1:51=C2=A0am, r.... [at] aist.go.jp (Raymond Wan) wrote:
>> I was wondering if there is a way for Perl to give me a warning if I
>> redeclare a variable in a different scope (and thus masking the outer
>> one). =C2=A0Just spent some time debugging this (which was obviously not=
my
>> intention to give two variables the same name)...and I guess it is a
>> silly mistake that I would do again and again. =C2=A0:-)
> Not that I'm aware of but you could use B::Xref =C2=A0to
> generate a cross reference.
>
> perl -MO=3DXref -wle 'my $foo=3D0; { my $foo =3D 1; }'
> ...
> =C2=A0 =C2=A0 =C2=A0$foo =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0=
i1, i1 =C2=A0 =C2=A0 =C2=A0<---- 2 instances of '$foo'


Thank you for your suggestion!

Oddly enough, when I run B::Xref through my scripts, I get
"segmentation faults" I don't know if I'm using it incorrectly -- but
on a simple Perl file, it works ok. And of course, it works ok with
your one liner above.

Hopefully it isn't a problem with my scripts -- which seem to be
running ok... Thank you!

Ray

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Raymond Wan [ Fr, 28 Januar 2011 05:33 ] [ ID #2054032 ]

Re: Redeclaration of variable [different scope]

On 11-01-27 11:33 PM, Raymond Wan wrote:
> Oddly enough, when I run B::Xref through my scripts, I get
> "segmentation faults" I don't know if I'm using it incorrectly -- but
> on a simple Perl file, it works ok. And of course, it works ok with
> your one liner above.

I get the same thing, even on small scripts.

--
Just my 0.00000002 million dollars worth,
Shawn

Confusion is the first step of understanding.

Programming is as much about organization and communication
as it is about coding.

The secret to great software: Fail early & often.

Eliminate software piracy: use only FLOSS.

--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Shawn H Corey [ Fr, 28 Januar 2011 13:59 ] [ ID #2054034 ]
Perl » gmane.comp.lang.perl.beginners » Redeclaration of variable [different scope]

Vorheriges Thema: help to capture the elements, every 8th, followed by 7 elements
Nächstes Thema: need to include and install CPAN modules as part of application stack